//
//  HMCouponsController.m
//  HMMarket
//
//  Created by 孙鹏飞 on 2017/6/14.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMCouponsController.h"
#import "CouponModel.h"
#import "CouponsCell.h"

@interface HMCouponsController () <UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray * couponArray;

@property (nonatomic, strong) UIButton * lookBtn;

@property (nonatomic, strong) UIView * botView;

@end

@implementation HMCouponsController

- (NSMutableArray *)couponArray
{
    if (_couponArray == nil) {
        _couponArray = [NSMutableArray array];
    }
    return _couponArray;
}

- (UIView *)botView
{
    if (_botView == nil) {
        _botView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidthSCREEN, 50)];
        _botView.backgroundColor = kBackGolor;
        
        _lookBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _lookBtn.frame = CGRectMake(0, 0, 120, 30);
        _lookBtn.centerX = _botView.centerX;
        [_lookBtn setTitle:@"查看过期优惠券" forState:UIControlStateNormal];
        [_lookBtn setTitleColor:kMainColor forState:UIControlStateNormal];
        _lookBtn.backgroundColor = kBackGolor;
        _lookBtn.titleLabel.font = [UIFont boldSystemFontOfSize:12];
        _lookBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_lookBtn addTarget:self action:@selector(lookCoupons) forControlEvents:UIControlEventTouchUpInside];
        [_botView addSubview:_lookBtn];
    }
    return _botView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"我的优惠券";
    [self setupTableView];
    [self setupData];
}

- (void)setupTableView
{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = kBackGolor;
    self.tableView.showsVerticalScrollIndicator = NO;
    
}

- (void)setupData
{
    // 1. 上传参数
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    [param setObjOrNil:[MGAccountTool getUid] forKey:@"uid"];
    
    // 2. 发送请求
    [HttpBaseTool get:kCouponList params:param success:^(id responseObj) {
        // 字典数组 -> 模型数组
        CouponModel * model = [CouponModel mj_objectWithKeyValues:responseObj[@"data"]];
        self.couponArray = model.list;
      
        // 刷新表格
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        if (error.code != NSURLErrorCancelled) { // 并非是取消任务导致的eror，其他错误导致的
            [HMHUDManager showHUDWithText:NetWorkError];
        }
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.couponArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CouponsCell * cell = [CouponsCell cellWithTableView:tableView];
    cell.detail = self.couponArray[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 118;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return self.botView;
}

- (void)lookCoupons
{
    // 1. 上传参数
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    [param setObjOrNil:[MGAccountTool getUid] forKey:@"uid"];
    [param setObjOrNil:@"2" forKey:@"status"];
    
    
    // 2. 发送请求
    [HttpBaseTool get:kCouponList params:param success:^(id responseObj) {
        // 字典数组 -> 模型数组
        CouponModel * model = [CouponModel mj_objectWithKeyValues:responseObj[@"data"]];
        NSArray * outTimeArray = model.list;
        [self.couponArray addObjectsFromArray:outTimeArray];
        
        self.botView.hidden = YES;
        // 刷新表格
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        if (error.code != NSURLErrorCancelled) { // 并非是取消任务导致的eror，其他错误导致的
            [HMHUDManager showHUDWithText:NetWorkError];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
